const navigateTo = (opt) => {
	if (checkSwitchPage(opt.url)) {
		uni.switchTab(opt)
	} else {
		uni.navigateTo(opt)
	}
};
const redirectTo = (opt) => {
	if (checkSwitchPage(opt.url)) {
		uni.switchTab(opt)
	} else {
		uni.redirectTo(opt)
	}

};
const reLaunch = (opt={url:'/pages/index/index'}) => {
	uni.reLaunch(opt)
};

const navigateBack = (opt = {}) => {
	if (getCurrentPages().length <= 1) {
		reLaunch({
			url: '/pages/index/index'
		});
	} else {
		uni.navigateBack(opt)
	}
};
const toLogin = () => {
	navigateTo({
		url: '/pages/login/index'
	})
}

const tabBarLinks = [
	'/pages/index/index',
	'/pages/category/index',
	'/pages/flow/index',
	'/pages/user/index',
];

/**
 * 获取tabBar页面路径列表
 */
const getTabBarLinks = () => {
	return tabBarLinks;
};
const checkSwitchPage = (url) => {
	const checkUrl = String(url).split('?')[0];
	return tabBarLinks.indexOf(checkUrl) >= 0;
}
export default {
	navigateTo,
	redirectTo,
	reLaunch,
	checkSwitchPage,
	navigateBack,
	getTabBarLinks,
	toLogin
}
